package com.lagou.edu.interceptor;

import com.lagou.edu.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private LoginService loginService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestUri = request.getRequestURI();
        if (requestUri.startsWith("/login") || requestUri.startsWith("/logout") || loginService.isLogin(request.getSession())) {
            return true;
        } else {
            response.sendRedirect("/login");
            return false;
        }
    }
}